/*$('.select-all-checkbox').on('ifChecked', function(event){
    $("input[name='select_id[]']").iCheck('check');
});
$('.select-all-checkbox').on('ifUnchecked', function(event){
    $("input[name='select_id[]']").iCheck('uncheck');
});*/

$(function () {
    /*$('input[type="checkbox"]').iCheck({
        checkboxClass: 'icheckbox_minimal-blue'
    });*/

    //悬浮菜单
    if($('#dbshop-menu-top').length > 0) {
        var stickyNavigationTop = $('#dbshop-menu-top').offset().top;
        $(window).scroll(function(){
            if ($(window).scrollTop() > stickyNavigationTop) {
                $('#dbshop-menu-top').addClass('navbar-fixed-top');
                $('#dbshop-menu-top').addClass('dbshop-top-menu');
            } else {
                $('#dbshop-menu-top').removeClass('navbar-fixed-top');
                $('#dbshop-menu-top').removeClass('dbshop-top-menu');
            }
        });
    }

    $(".select-all-checkbox").click(function () {
        var checkState = this.checked;
        if(!checkState) {
            $("input[name='select_id[]']").prop("checked", false);
            $(".select-all-checkbox").prop("checked", false);
        } else {
            $("input[name='select_id[]']").prop("checked", true);
        }
    });


});

//选择商品分类
function selectClassId(classPath, classId) {
    if($('#classId'+classId).prop("checked")) {
        let classArray  = new Array();
        classArray      = classPath.split(",");
        for(let i=0; i<classArray.length; i++) {
            if(classArray[i] != classId) $('#classId'+classArray[i]).prop('checked', 'true');
        }
    }
}

//通用获取列表方法
function dbshopAjaxList(listUrl,showDivDd) {
    $.get(listUrl, {}, function(html){
        $("#"+showDivDd).html(html);
    });
}

/**
 * 通用提示信息
 * @param msg
 */
function shopMessage(msg) {
    layer.msg(msg);
}

function deleteNotConfirm(url, deleteId) {
    $.get(url, {}, function (data) {
        if(data.state == 'ok') {
            if(deleteId.length >0) $('#'+deleteId).remove();
            else window.location.reload();
        }
        else {
            if(data.hasOwnProperty("msg")) {
                layer.msg(data.msg);
            }
        }
    }, 'json');
}
/**
 * 删除问询
 * @param msg
 * @param url
 * @param toUrlState
 */
function deleteConfirm(msg, url, toUrlState) {
    layer.confirm(msg, {}, function () {
        if(toUrlState == 'false') {
            $.get(url, {}, function (data) {
                if(data.state == 'ok') window.location.reload();
                else {
                    if(data.hasOwnProperty("msg")) {
                        layer.msg(data.msg);
                    } else window.location.reload();
                }
            }, 'json');
        } else window.location.href = url;
    })
}

//加法函数
function accAdd(arg1, arg2) {
    let r1, r2, m;
    try {
        r1 = arg1.toString().split(".")[1].length;
    }
    catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    }
    catch (e) {
        r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));

    return (arg1 * m + arg2 * m) / m;
    //return advFormatNumber((arg1 * m + arg2 * m) / m, 2);
}

//减法函数
function Subtr(arg1, arg2) {
    let r1, r2, m, n;
    try {
        r1 = arg1.toString().split(".")[1].length;
    }
    catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    }
    catch (e) {
        r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));
    //last modify by deeka
    //动态控制精度长度
    n = (r1 >= r2) ? r1 : r2;
    return ((arg1 * m - arg2 * m) / m).toFixed(n);
    //return advFormatNumber(((arg1 * m - arg2 * m) / m).toFixed(n), 2);
}


//乘法函数
function accMul(arg1, arg2) {
    let m = 0, s1 = arg1.toString(), s2 = arg2.toString();
    try {
        m += s1.split(".")[1].length;
    }
    catch (e) {
    }
    try {
        m += s2.split(".")[1].length;
    }
    catch (e) {
    }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
    //return advFormatNumber(Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m), 2);
}

//除法函数
function accDiv(arg1, arg2) {
    let t1 = 0, t2 = 0, r1, r2;
    try {
        t1 = arg1.toString().split(".")[1].length;
    }
    catch (e) {
    }
    try {
        t2 = arg2.toString().split(".")[1].length;
    }
    catch (e) {
    }
    with (Math) {
        r1 = Number(arg1.toString().replace(".", ""));
        r2 = Number(arg2.toString().replace(".", ""));
        return (r1 / r2) * pow(10, t2 - t1);
        //return advFormatNumber((r1 / r2) * pow(10, t2 - t1), 2);
    }
}
/* *
 * 截取小数位数
 */
function advFormatNumber(value, num) // 四舍五入
{
    let a_str = formatNumber(value, num);
    let a_int = parseFloat(a_str);
    if (value.toString().length > a_str.length)
    {
        let b_str = value.toString().substring(a_str.length, a_str.length + 1);
        let b_int = parseFloat(b_str);
        if (b_int < 5)
        {
            return a_str;
        }
        else
        {
            let bonus_str, bonus_int;
            if (num == 0)
            {
                bonus_int = 1;
            }
            else
            {
                bonus_str = "0.";
                for (let i = 1; i < num; i ++ )
                    bonus_str += "0";
                bonus_str += "1";
                bonus_int = parseFloat(bonus_str);
            }
            a_str = formatNumber(a_int + bonus_int, num);
        }
    }
    return a_str;
}

function formatNumber(value, num)
{
    let a, b, c, i;
    a = value.toString();
    b = a.indexOf('.');
    c = a.length;
    if (num == 0)
    {
        if (b != - 1)
        {
            a = a.substring(0, b);
        }
    }
    else
    {
        if (b == - 1)
        {
            a = a + ".";
            for (i = 1; i <= num; i ++ )
            {
                a = a + "0";
            }
        }
        else
        {
            a = a.substring(0, b + num + 1);
            for (i = c; i <= b + num; i ++ )
            {
                a = a + "0";
            }
        }
    }
    return a;
}

/**
 * 笛卡尔积换算
 * @param {array} [arguments] 多个数组, 如：[[1, 2, 3, 4], [11, 22, 33, 44]]
 * @author Flc
 * @example
 *     descartes([[1, 2, 3, 4], [11, 22, 33, 44], [111, 222, 333, 444]]);
 */
function descartes(args) {
    //var args = arguments;
    let rs = [];

    // A. 校验并转换为JS数组
    for (let i = 0; i < args.length; i++) {
        if (!$.isArray(args[i])) {
            return false;  // 参数必须为数组
        }
    }

    // 两个笛卡尔积换算
    let bothDescartes = function (m, n){
        let r = [];
        for (let i = 0; i < m.length; i++) {
            for (let ii = 0; ii < n.length; ii++) {
                let t = [];
                if ($.isArray(m[i])) {
                    t = m[i].slice(0);  //此处使用slice目的为了防止t变化，导致m也跟着变化
                } else {
                    t.push(m[i]);
                }
                t.push(n[ii]);
                r.push(t);
            }
        }
        return r;
    };

    // 多个笛卡尔基数换算
    for (var i = 0; i < args.length; i++) {
        if (i == 0) {
            rs = args[i];
        } else {
            rs = bothDescartes(rs, args[i]);
        }
    }

    return rs;
}